//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//   PACKAGE : dcf
//   FILE    : Endpoint.java
//   CLASS   : Endpoint
//   DATE    : 2006-04-01
//   
//   DESCRIPTION
//   The Endpoint class represents one side of a communication channel. An endpoint consists of a host 
//   identifier (IP address) and a port. Once an endpoint is created, it cannot be modified.
//   
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//   Copyright (c) 2006 by Hal Smith
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this source code 
//   (the "Code")to deal with the Code without restriction, including without limitation the rights to use, copy,
//   modify, merge, publish and distribute the Code, and to permit persons to whom the Code is furnished to do 
//   so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial 
//   portions of the Software.
//   
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//   VERSION HISTORY
//
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package dcf;


public class Endpoint implements java.io.Serializable {
	
	// The host and port at which the endpoint is listening.
	//
	private String host;
	private int    port;

	
	// Constructor.
	//
	// This operation initializes the endpoint. The host value must be an IP address (four octal values
	// separated by periods, i.e., "11.22.33.44"). The port value must be between 0 and 65535. If either
	// value is invalid, an InitializationException will be thrown. There are no other exceptions thrown
	// by this operation.
	//
	public Endpoint(String initHost, int initPort) throws InitializationException {
	
		host = initHost;
		port = initPort;
		
		// Ensure the host and port values are valid. If not, log an error and throw and exception.
		//
	    boolean errorMessageStarted = false;

	    if (initHost == null) {
			
			// Log the initHost is null message
	    	//
	    	errorMessageStarted = true;
	    	
			System.out.println("Endpoint.Endpoint(host, port):");
			System.out.println("   Supplied host is null");
			
			throw new InitializationException();
	    	
			
		} else {
			
			// Validate the structure of the IP address
			// 
			String values[] = new String[4];
			
			int index = initHost.indexOf('.');
			int prevIndex = 0;
			values[0] = initHost.substring(prevIndex, index);

			prevIndex = index+1;
			index = initHost.indexOf('.', prevIndex);
			values[1] = initHost.substring(prevIndex, index);

			prevIndex = index+1;
			index = initHost.indexOf('.', prevIndex);
			values[2] = initHost.substring(prevIndex, index);
			
			prevIndex = index+1;
			values[3] = initHost.substring(prevIndex, initHost.length());
			
			
			
			if ((values.length != 4) || 
				(Integer.parseInt(values[0]) < 0 || Integer.parseInt(values[0]) > 255) ||
				(Integer.parseInt(values[1]) < 0 || Integer.parseInt(values[1]) > 255) ||
				(Integer.parseInt(values[2]) < 0 || Integer.parseInt(values[2]) > 255) ||
				(Integer.parseInt(values[3]) < 0 || Integer.parseInt(values[3]) > 255) ) {
				
				
				if (!errorMessageStarted) {
					
					errorMessageStarted = true;

					System.out.println("Endpoint.Endpoint(host, port):");
					
				}
				
				System.out.println("   Host value is not a valid IP address.");

				throw new InitializationException();

			} // if (values.length != 4) ...		
		
		} // if (initHost == null) ...
	    
	    
	    if ((initPort < 0) || (initPort > 65535)) {
	    	
	    	if (!errorMessageStarted) {
	    		
	    		errorMessageStarted = true;
	    		
				System.out.println("Endpoint.Endpoint(host, port):");
	    		
	    	}
	    	
			System.out.println("   Port value not in the interval [0, 65535].");
			
			throw new InitializationException();
	    		    	
	    } // if (initPort < 0 || initPort > 65535) ...
	    
	    
	} // constructor 

	
	// This operation returns a string version of the host identifier. There are no exceptions thrown from
	// this operation.
	//
	public String getHost()	{

		return host;
	
	} // getHost()

	
	// This operation returns the port number as an integer. There are no exceptions thrown from this 
	// operation.
	//
	public int getPort() {

		return port;
	
	} // getPort()
	
	
	public boolean equals(Object obj) {
		
		if (obj instanceof Endpoint) {
		   
		   Endpoint rhs = (Endpoint)obj;
		   
		   return ( (host.equals(rhs.host)) && (port==rhs.port) );
		
		} else {
			
			return false;
		}

	}
	

}
